<?php
class Page{
	private $pageSize = 0;		//每页显示的条数
	private $currentPage = 1;		//当前页的页码
	private $totalPage;		//总页数
	
	#
	#构造函数
	#@param:$pageSize,$totalCount
	#@$pageSize:每页显示的条数
	#@$totalCount:总共有多少条记录
	#
	function __construct($pageSize,$totalCount){
		//总页数（最后一页的页码）
		$endPage = ceil($totalCount/$pageSize);
		$this->totalPage = $endPage;
		$this->pageSize = $pageSize;
		//页码合法性判断，page没有传或page<=0或page>totalPage
		if(isset($_GET['page']))
			$page=$_GET['page'];
		if(empty($page)||$page<=0){
			$page=1;
		}
		if($page>$endPage){
			$page=$endPage;
		}
		//得到当前页
		$this->currentPage=$page;
	}

	#
	#函数功能：得到url地址栏后面的参数部分，用"&"符号链接
	#
	function getUrlParam(){
		$urlStr="";//url问号传值部分
		foreach ($_GET as $key=>$value){
			if($key!="page"){
				$urlStr.="&".$key."=".$value;
			}
		}
	//	$urlStr=substr($urlStr,1);
		return $urlStr;
	}

	#
	#作用：得到查询语句中limit子句后面的开始位置
	#
	function getStart(){	
		$start=($this->currentPage-1)*$this->pageSize;
		return $start;	
	}
	#
	#作用：得到分页的数字部分
	#@param:两边显示的个数  默认为3 eg:若为3，则数字部分显示的个数为2*3+1=7个
	#
	function getPageStr1($geNum=3){
		//前后各显示3个，共7个
		$pageStr="";
		$totalNum = 2*$geNum + 1;
		if($this->totalPage<=$totalNum){//总页数小于等于7
			for($i=1;$i<=$this->totalPage;$i++){
				if($i==$this->currentPage){
					$pageStr.="<a href='?page={$i}{$this->getUrlParam()}' class='cur'>&nbsp;{$i}&nbsp;</a>";
				}else{
					$pageStr.="<a href='?page={$i}{$this->getUrlParam()}'>&nbsp;{$i}&nbsp;</a>";
				}
			}
		}else{
			if($this->currentPage<=($geNum + 1)){//前面不够显示（当前页页码小于等于4），从1开始显示
				for($i=1;$i<=$totalNum;$i++){
					if($i==$this->currentPage){
						$pageStr.="<a href='?page={$i}{$this->getUrlParam()}' class='cur'>&nbsp;{$i}&nbsp;</a>";
					}else{
						$pageStr.="<a href='?page={$i}{$this->getUrlParam()}'>&nbsp;{$i}&nbsp;</a>";
					}
				}
			}else if($this->currentPage+$geNum > $this->totalPage){//后面不够显示（当前页码+后面显示的个数>总页数），从后往前显示7个
				for($i=$this->totalPage-2*$geNum;$i<=$this->totalPage;$i++){
					if($i==$this->currentPage){
						$pageStr.="<a href='?page={$i}{$this->getUrlParam()}' class='cur'>&nbsp;{$i}&nbsp;</a>";
					}else{
						$pageStr.="<a href='?page={$i}{$this->getUrlParam()}'>&nbsp;{$i}&nbsp;</a>";
					}
				}
			}else{
			for($i=$this->currentPage-$geNum;$i<=$this->currentPage+$geNum;$i++){
					if($i==$this->currentPage){
						$pageStr.="<a href='?page={$i}{$this->getUrlParam()}' class='cur'>&nbsp;{$i}&nbsp;</a>";
					}else{
						$pageStr.="<a href='?page={$i}{$this->getUrlParam()}'>&nbsp;{$i}&nbsp;</a>";
					}
				}
			}
		}
		return $pageStr;
	}

	#
	#作用：得到完整的分页栏
	#
	#
	function getPageStr(){
		$str=$this->getUrlParam();
		if($this->currentPage == $this->totalPage)
			$nextPage=$this->totalPage;
		else 
			$nextPage=$this->currentPage+1;
		if($this->currentPage ==1)
			$lastPage=$this->currentPage;
		else 
			$lastPage=$this->currentPage-1;
		$strN="<link rel='stylesheet' type='text/css' href='../Public/Css/page.css'><div>&nbsp;
		<a href='?page=1{$str}'>首页</a>&nbsp;&nbsp;<a href='?page={$lastPage}{$str}'><<&nbsp;上一页</a>
		&nbsp;&nbsp;{$this->getPageStr1()}<a href='?page={$nextPage}{$str}'>&nbsp;&nbsp;下一页&nbsp;>></a>
		&nbsp;&nbsp;<a href='?page={$this->totalPage}{$str}'>尾页&nbsp;&nbsp;</a>第{$this->currentPage}/{$this->totalPage}页
		<form action='' method='get'><input type='text' name='page' size='2' value='1'/><input type='submit' background='but1.gif' value='GO'></form></div>";
		return $strN;
	}

}
